onclick for dynamically created element jquery

63

onclick for dynamically created element jquery -

$('body').on('click', 'a.myclass', function() {
    // do something
});

dynamic button click event in jquery -

var counter = 0;

$("button").click(function() {
    $("h2").append("<p class='test'>click me " + (++counter) + "</p>")
});

// With on():

$("h2").on("click", "p.test", function(){
    alert($(this).text());
});

Comments

Submit
0 Comments